//引入express框架，设置端口号为3000
const express = require('express');
const app = express();
const port = 3000;

//输出hello world
app.get('/', (req, res) => {
    res.send('Hello World!');
});



//引入index文件
require('./router/index')(app);

//设置跨域访问
app.all('*', (req, res, next) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.header("X-Powered-By",' 3.2.1')
    res.header("Content-Type", "application/json;charset=utf-8");
    next();
  });

//404找不到页面
app.use(function(req, res, next){
    let err = new Error('Not Found!');
    err.status = 404;
    next(err);
});

//500出错页面
app.use(function(err,req,res,next){
    res.status(err.status || 500);
    res.send(err.message);
});

//监听端口
app.listen(port, () => {
    console.log(`Example app listening at http://localhost:${port} !`);
});

